package com.hmall.item.web;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.hmall.common.dto.PageDTO;
import com.hmall.common.response.ItemUpdateStockVO;
import com.hmall.item.pojo.Item;

import com.hmall.item.pojo.ItemDTO;
import com.hmall.item.service.IItemService;
//import org.springframework.amqp.rabbit.core.RabbitTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.amqp.RabbitTemplateConfigurer;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@RequestMapping("item")
@Slf4j
public class ItemController {

    @Resource
    private IItemService itemService;


    /**
     * 商品分页查询
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/list")
    public PageDTO page(@RequestParam("page")Integer page,@RequestParam("size")Integer size){
        Page<Item> page1 = itemService.page(new Page<>(page, size));
        return new PageDTO(page1.getTotal(),page1.getRecords());
    }

    /**
     * 根据id查询商品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Item getById(@PathVariable Long id){
        return itemService.getById(id);
    }

    /**
     * 新增商品
     * @param itemDTO
     */
    @PostMapping()
    public void add(@RequestBody ItemDTO itemDTO){
        Item item = new Item();
        BeanUtils.copyProperties(itemDTO,item);
        item.setCommentCount(0);
        item.setStatus(2);
        item.setCreateTime(new Date());
        item.setCreateTime(new Date());
        itemService.save(item);
    }

    /**
     * 修改商品
     * @param item
     */
    @PutMapping
    public void updateItem(@RequestBody Item item ){
        log.info("修改商品：{}",item);
        itemService.update(item);
    }

    /**
     * 删除商品
     * @param id
     */
    @DeleteMapping("{id}")
    public void  deleteItem(@PathVariable("id") Long id){
        log.info("删除商品：{}",id);
        itemService.delete(id);
    }



    /**
     * 商品的上下架
     * @param id
     * @param status
     */
    @PutMapping("/status/{id}/{status}")
    public void updateStatus(@PathVariable("id") Long id,@PathVariable("status") Integer status){
        log.info("商品上下架：status{},id{}",status,id);
        itemService.changeStatus(id,status);

    }

    /**
     * 扣减商品库存
     *
     * @param itemId 商品id
     * @param num    扣减数量
     */
    @PutMapping("/deleteStock/{itemId}/{num}")
    public ItemUpdateStockVO updateStock(@PathVariable("itemId") Long itemId, @PathVariable("num") Integer num) {
        log.info("扣减库存，商品id：{},扣减数量：{}", itemId, num);
        ItemUpdateStockVO result = itemService.updateStock(itemId, num);
        return result;
    }

    /**
     * 恢复商品库存
     * @param itemId
     * @param num
     * @return
     */
    @PutMapping("/addStock/{itemId}/{num}")
    public ItemUpdateStockVO addStock(@PathVariable("itemId") Long itemId, @PathVariable("num") Integer num) {
        log.info("恢复库存，商品id：{},恢复数量：{}", itemId, num);
        ItemUpdateStockVO result = itemService.addStock(itemId, num);
        return result;
    }



}
